<?php
namespace app\admin\model;
use think\Model;
use think\Db;

/**
 * 通道黑名单关键字模型
 * @package app\admin\model
 */
class PlatBlackKeyword extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'bus_black_keyword';

    /**
     * [getAlls 条件查询全部数据]
     * @author   .guo
     * @DataTime 2019-05-27
     * @param    [type]     $request [条件数组]
     * @return   [type]              [object]
     */
    public static function getAlls($request)
    {
        $where = [];

        if (!empty($request['keyword'])) {

            $where[] = ['b.keyword', 'like', '%' . trim($request['keyword']) . '%'];
        }

        if (!empty($request['plat_id'])) {

            $where[] = ['b.plat_id', 'eq', trim($request['plat_id'])];
        }

        return self::alias('b')
            ->field('b.*,bp.platfrom')
            ->where($where)
            ->join('bus_platfrom bp', 'b.plat_id = bp.plat_id', 'LEFT')
            ->paginate($request['limit'], false, $request)
            ->each(function($item, $key) {

                switch ($item->onoff) {
                    case '1':
                        $item->onoff_ = '<span class="layui-badge layui-bg-green status-qy">开启</span>';
                        break;
                    default:
                        $item->onoff_ = '<span class="layui-badge status-jy">禁用</span>';
                        break;
                }
            });
    }

    public static function getOne($data)
    {
        $where = [
            'plat_id' => $data['plat_id'],
            'keyword' => $data['keyword']
        ];
        return self::where($where)->find();
    }

    public static function add($data)
    {
        return self::create($data);
    }

    public static function upd($data,$id)
    {
        return self::where('id',$id)->update($data);
    }

    public static function openOrClose($black_id,$status)
    {
        trace(session('username').'把黑名单关键字id为'.$black_id.'的开启状态改为了'.$status,'info');
        return self::where(['id'=>$black_id])->update(['onoff' => $status]);
    }
}
